const {Users} = require('./db')
const {Admins} = require('./db')
const {Finance} = require('./db')
const {Equipments} = require('./db')
const {Receptions} = require('./db')


//人事管理

//根据人事名查找用户
const findUser = (username) =>{
    return Users.findOne({username})
}
//根据id查找人事
const findOne = (id) =>{
    return Users.findOne({_id:id})
}
//注册人事
const signUsers = ({username,Tel,role,salary,state})=>{
    const users = new Users({
        username,
        Tel,
        role,
        salary,
        state,
    })
    return users.save()
    
}
//显示人事列表
const findList = ()=>{
    return Users.find().sort({_id:-1})
}
//删除人事
const remove = (id)=>{
    return Users.findByIdAndRemove(id)
}
//模糊查询人事
const findGrabbleName = (username) => {
    return Users.find({username:{$regex:`${username}`}})
}
//修改人事
const upDate = async({id,username,Tel,role,salary,state})=>{
    return Users.updateOne({
        _id: id
    }, {
        $set: {
            username,
            Tel,
            role,
            salary,
            state,
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
exports.signUsers = signUsers
exports.findUser = findUser
exports.findList = findList
exports.remove = remove
exports.findGrabbleName = findGrabbleName
exports.upDate = upDate
exports.findOne = findOne

//根据用户名查找用户
const findAdmins = (username) =>{
    return Admins.findOne({username})
}
//根据id查找用户
const findOneAdmins = (id) =>{
    return Admins.findOne({_id:id})
}
//注册用户
const zhuceAdmins = ({username,password,email,position})=>{
    const admins = new Admins({
        username,
        password,
        email,
        position,
    })
    return admins.save()
    
}
//显示用户列表
const findListAdmins = ()=>{
    return Admins.find().sort({_id:-1})
}
//删除用户
const removeAdmins = (id)=>{
    return Admins.findByIdAndRemove(id)
}
//模糊查询用户
const findGrabbleNameAdmins = (username) => {
    return Admins.find({username:{$regex:`${username}`}})
}
//修改用户
const upDateAdmins = async({id,username,email,position})=>{
    return Admins.updateOne({
        _id: id
    }, {
        $set: {
            username,
            email,
            position,
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}

exports.zhuceAdmins = zhuceAdmins
exports.findAdmins = findAdmins
exports.findListAdmins = findListAdmins
exports.removeAdmins = removeAdmins
exports.findGrabbleNameAdmins = findGrabbleNameAdmins
exports.upDateAdmins = upDateAdmins
exports.findOneAdmins = findOneAdmins

//显示财务列表
const findListFinance = ()=>{
    return Finance.find().sort({_id:-1})
}
//添加财务
const signFinance = ({ money,amountMoney,department})=>{
    const finances = new Finance({
        money,
        amountMoney,
        department,
    })
    return finances.save()
    
}
//删除财务
const removeFinance = (id)=>{
    return Finance.findByIdAndRemove(id)
}
//模糊查询财务
const findGrabbleNameFinance = (department) => {
    return Finance.find({department:{$regex:`${department}`}})
}
//根据id查找财务
const findOneFinance = (id) =>{
    return Finance.findOne({_id:id})
}
//修改财务
const upDateFinance = async({id,money,amountMoney,department})=>{
    return Finance.updateOne({
        _id: id
    }, {
        $set: {
            money,
            amountMoney,
            department,
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
exports.findListFinance = findListFinance
exports.signFinance = signFinance
exports.removeFinance = removeFinance
exports.findOneFinance = findOneFinance
exports.findGrabbleNameFinance = findGrabbleNameFinance
exports.upDateFinance = upDateFinance

//显示设备列表
const findListEquipments = ()=>{
    return Equipments.find().sort({_id:-1})
}
//添加设备
const signEquipments = ({ DeviceName,state,PurchaseDate,money,num})=>{
    const equipments = new Equipments({
        DeviceName,
        state,
        PurchaseDate,
        money,
        num,
    })
    return equipments.save()
    
}
//模糊查询设备
const findGrabbleNameEquipments = (DeviceName) => {
    return Equipments.find({DeviceName:{$regex:`${DeviceName}`}})
}
//删除设备
const removeEquipments = (id)=>{
    return Equipments.findByIdAndRemove(id)
}
//根据id查找设备
const findOneEquipments = (id) =>{
    return Equipments.findOne({_id:id})
}
//修改设备
const upDateEquipments = async({id,DeviceName,state,PurchaseDate,money,num})=>{
    return Equipments.updateOne({
        _id: id
    }, {
        $set: {
            DeviceName,
            state,
            PurchaseDate,
            money,
            num,
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
exports.findListEquipments = findListEquipments
exports.signEquipments = signEquipments
exports.findGrabbleNameEquipments = findGrabbleNameEquipments
exports.removeEquipments = removeEquipments
exports.findOneEquipments = findOneEquipments
exports.upDateEquipments = upDateEquipments
//显示用户列表
const findListReceptions = ()=>{
    return Receptions.find().sort({_id:-1})
}
//模糊查询设备
const findGrabbleNameReceptions = (Customerlist) => {
    return Receptions.find({Customerlist:{$regex:`${Customerlist}`}})
}
//删除设备
const removeReceptions = (id)=>{
    return Receptions.findByIdAndRemove(id)
}
//添加设备
const signReceptions = ({ Customerlist,identity,affair,speedprogress})=>{
    const equipments = new Receptions({
        Customerlist,
        identity,
        affair,
        speedprogress,
    })
    console.log(Customerlist);
    return equipments.save()
    
}
//根据id查找设备
const findOneReceptions = (id) =>{
    return Receptions.findOne({_id:id})
}
//修改设备
const upDateReceptions = async({id,Customerlist,identity,affair,speedprogress})=>{
    return Receptions.updateOne({
        _id: id
    }, {
        $set: {
            Customerlist,
            identity,
            affair,
            speedprogress,
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
exports.findListReceptions = findListReceptions
exports.findGrabbleNameReceptions = findGrabbleNameReceptions
exports.removeReceptions = removeReceptions
exports.signReceptions = signReceptions
exports.findOneReceptions = findOneReceptions
exports.upDateReceptions = upDateReceptions